export const getLocation = (options) => {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			...options,
			success(res) {
				resolve(res)
			},
			fail(err) {
				console.log('获取地址出错了...', err)
				reject(err)
			}
		})
	})
	// .then(res => {
	// 	return res
	// }).catch(err => {
	// 	console.log('捕获到getLocation错误', err)
	// })
}

export const parsePath = (str) => {
	let url = ''
	const CATEGORY = 'frontCatalogProductClusterList'
	const DETAIL = 'productCluster'
	const ACTIVITY = 'activityProductClusters'
	const COUPON = 'couponCenter'
	const CATALOG = 'catalogProductClusterList'
	const PLUS = 'getPlus'
	const params = str.split('?')[1]
	if (str.includes(CATEGORY)) {
		url = '/pages/category/category?' + params
	} else if (str.includes(DETAIL)) {
		url = '/pages/detail/detail?' + params
	} else if (str.includes(ACTIVITY)) {
		url = '/pages/activity/activity?' + params
	} else if (str.includes(COUPON)) {
		url = '/pages/coupon/coupon?' + params
	} else if (str.includes(CATALOG)) {
		url = '/pages/catalog/catalog?' + params
	} else if (str.includes(PLUS)) {
		url = '/pages/plus/plus?' + params
	}
	return url
}

export const extractParams = (str) => {
	// /pages/category/category?storeNo=133&catalogId=1272893
	// storeNo=133&catalogId=1272893
	const paramsObj = {}
	const paramsStr = str.split('?')[1]
	paramsStr.split('&').forEach(item => {
		const keyValueArr = item.split('=')
		paramsObj[keyValueArr[0]] = keyValueArr[1]
	})
	return paramsObj
}

export const groupArray = (arr, length) => {
	const chunk = length;
	const len = arr.length;
	if (len <= length) return [arr]
	let result = []
	for (let i = length; i < len; i += chunk) {
		result.push(arr.slice(i, i + chunk)) // 每10项分成一组        
	}
	return result
}

export const formatIndexPageArray = (tabBlockRes) => {

	const _formatArray = (list, tabBlock, length) => {
		return groupArray(list, length).map((e, i) => {
			return {
				title: '',
				articles: [],
				products: e,
				pageBlockId: tabBlock.pageBlockId + i + 1,
				blockType: tabBlock.blockType
			}
		})
	}

	tabBlockRes.blocks.map((item) => {
		if (item.blockType == 3) {
			// #ifdef H5 || MP-WEIXIN
			item.products = item.products.slice(0, 9)
			// #endif
			// #ifdef APP-PLUS
			item.products = item.products.slice(0, 5)
			// #endif
			// item.moreProducts = item.products.slice(10)
			return item
		} else if (item.blockType == 4) {
			tabBlockRes.moreBlocks = _formatArray(item.products, item, 1)
			item.products = []
			return item
		} else if (item.blockType == 5) {
			tabBlockRes.moreBlocks = _formatArray(item.products, item, 2)
			item.products = []
			return item
		} else {
			return item
		}
	})

	return tabBlockRes
}


export const getHtmlImageUrlList = (str) => {
	const patt = /<img[^>]+src=['"]([^'"]+)['"]+/g;
	const result = []
	let temp
	while ((temp = patt.exec(str)) != null) {
		result.push(temp[1]);
	}
	return result
}
